home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / TestParts / Clock / ClockObj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  14.0 KB  |  570 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ClockObj.cpp
  3.  
  4.     Contains:    Object accessor implementation for the AppleTest_ClockPart
  5.  
  6.     Owned by:    Jon Pugh
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <8>     11/2/95    RR        Renamed module AppleTestClock->AppleTest
  13.          <7>    25/09/95    NP        1979071: Check results of operator new.
  14.          <6>     9/11/95    NP        1272294: Messaging code cleanup.
  15.          <5>      9/4/95    TJ        Added Includes to Compile with out
  16.                                     PC-Headers.
  17.          <4>     7/12/95    TJ        Moved declerations out of switch statement
  18.                                     so that it would compile with SCpp.
  19.          <3>     7/10/95    JP        Scripting cleanup
  20.          <2>     6/30/95    JP        Accessor foo
  21.          <1>     6/13/95    JP        first checked in
  22.     To Do:
  23. */
  24.  
  25. #ifndef _ALTPOINT_
  26. #include "AltPoint.h"            /* Use C++ savvy ODPoint and ODRect */
  27. #endif
  28.  
  29. #ifndef _ODDESUTL_
  30. #include <ODDesUtl.h>
  31. #endif
  32.  
  33. #ifndef SOM_ODOSLToken_xh
  34. #include <ODOSLTkn.xh>
  35. #endif
  36.  
  37. #ifndef SOM_ODAppleEvent_xh
  38. #include <ODAplEvt.xh>
  39. #endif
  40.  
  41. #ifndef _ORDCOLL_
  42. #include "OrdColl.h"
  43. #endif
  44.  
  45. #ifndef _CLOCKOBJ_
  46. #include "ClockObj.h"
  47. #endif
  48.  
  49. #ifndef _CLOCKDEF_
  50. #include "ClockDef.h"
  51. #endif
  52.  
  53. #ifndef _CLKPRPAC_
  54. #include "ClkPrpAc.h"
  55. #endif
  56.  
  57. #ifndef _CLOCKPRT_
  58. #include "ClockPrt.h"
  59. #endif
  60.  
  61. //#ifndef _DFRMITER_
  62. //#include "DFrmIter.h"
  63. //#endif
  64.  
  65. #ifndef SOM_ODDraft_xh
  66. #include "Draft.xh"
  67. #endif
  68.  
  69. #ifndef SOM_Module_AppleTest_defined
  70. #include "Clock.xh"
  71. #endif
  72.  
  73. #ifndef _EXCEPT_
  74. #include "Except.h"
  75. #endif
  76.  
  77. #ifndef _ODDEBUG_
  78. #include "ODDebug.h"
  79. #endif
  80.  
  81. #ifndef SOM_ODFacet_xh
  82. #include "Facet.xh"
  83. #endif
  84.  
  85. #ifndef SOM_ODFrame_xh
  86. #include "Frame.xh"
  87. #endif
  88.  
  89. #ifndef __LIMITS__
  90. #include <limits.h>
  91. #endif
  92.  
  93. #ifndef SOM_ODNameResolver_xh
  94. #include "NamRslvr.xh"
  95. #endif
  96.  
  97. #ifndef SOM_ODPart_xh
  98. #include "Part.xh"
  99. #endif
  100.  
  101. #ifndef SOM_CLOCKSI_xh
  102. #include "ClockSI.xh"
  103. #endif
  104.  
  105. #ifndef _SIHELPER_
  106. #include "SIHelper.h"
  107. #endif
  108.  
  109. #ifndef _ODREGISTRY_
  110. #include "ODRgstry.xh"
  111. #endif
  112.  
  113. #ifndef SOM_ODSession_xh
  114. #include "ODSessn.xh"
  115. #endif
  116.  
  117. #ifndef _SEUTILS_
  118. #include "SEUtils.h"
  119. #endif
  120.  
  121. #ifndef SOM_ODShape_xh
  122. #include "Shape.xh"
  123. #endif
  124.  
  125. #ifndef SOM_ODStorageUnit_xh
  126. #include "StorageU.xh"
  127. #endif
  128.  
  129. #ifndef SOM_ODTransform_xh
  130. #include "Trnsform.xh"
  131. #endif
  132.  
  133. #ifndef SOM_ODMessageInterface_xh
  134. #include "MssgIntf.xh"
  135. #endif
  136.  
  137. #ifndef SOM_ODFrameFacetIterator_xh
  138. #include "FrFaItr.xh"
  139. #endif
  140.  
  141. #ifndef _ODUTILS_
  142. #include "ODUtils.h"
  143. #endif
  144.  
  145. #ifndef __AEOBJECTS__
  146. #include <AEObjects.h>
  147. #endif
  148.  
  149. #ifndef __AEPACKOBJECT__
  150. #include <AEPackObject.h>
  151. #endif
  152.  
  153. #ifndef __AEREGISTRY__
  154. #include <AERegistry.h>
  155. #endif
  156.  
  157. #ifndef __ASREGISTRY__
  158. #include <ASRegistry.h>
  159. #endif
  160.  
  161. #ifndef __TEXTUTILS__
  162. #include <TextUtils.h>
  163. #endif
  164.  
  165. #include <string.h>
  166.  
  167. #pragma segment ClockObj
  168.  
  169.  
  170.  
  171. //==============================================================================
  172. // Function prototypes
  173. //==============================================================================
  174.  
  175. static void CreatePartObjectSpec(DescType keyForm, AEDesc* containerObjSpec,
  176.         AEDesc *objectSpec);
  177. void GetDirectParam(ODSession* session, AppleEvent* message,
  178.         AEDesc* evtDp);
  179.  
  180. void InstallStaticCallbacks(SIHelper* helper, ClockPart* clock)
  181. {
  182.     ODObjectAccessorUPP theAccessorUPP ;
  183.  
  184.     theAccessorUPP = NewODObjectAccessorProc( GetPropertyFromNULL ) ;
  185.     helper->InstallObjectAccessor(cProperty, typeNull, theAccessorUPP,
  186.                                         (ODSLong)clock);
  187.  
  188.     ODEventHandlerUPP theEventHandlerUPP ;
  189.  
  190.     theEventHandlerUPP = NewODEventHandlerProc( HandleSetData ) ;
  191.     helper->InstallEventHandler(kAECoreSuite, kAESetData,
  192.                                                 theEventHandlerUPP,
  193.                                                 (ODSLong)clock);
  194.  
  195.     theEventHandlerUPP = NewODEventHandlerProc( HandleGetData ) ;
  196.     helper->InstallEventHandler(kAECoreSuite, kAEGetData,
  197.                                                 theEventHandlerUPP,
  198.                                                 (ODSLong)clock);
  199.  
  200.     ODDisposeTokenUPP disposeTokenUPP
  201.             = NewODDisposeTokenProc( ClockDisposeToken ) ;
  202.     helper->InstallDisposeTokenProc( disposeTokenUPP, (ODSLong)clock);
  203. }
  204.  
  205.  
  206. //------------------------------------------------------------------------------
  207. // GetPropertyFromNULL
  208. //------------------------------------------------------------------------------
  209.  
  210. static pascal ODError GetPropertyFromNULL(    ODPart*                part,
  211.                                             DescType            desiredClass,
  212.                                             ODOSLToken*            container,
  213.                                             DescType            containerClass,
  214.                                             DescType            form,
  215.                                             ODDesc*                selectionData,
  216.                                             ODOSLToken*            value,
  217.                                             ODSLong                refCon)
  218. {
  219. ODUnused(desiredClass);
  220. ODUnused(container);
  221. ODUnused(containerClass);
  222.  
  223.     Environment*            ev = somGetGlobalEnvironment();
  224.     ClockPart*                clock = (ClockPart*)refCon;
  225.     ODSession*                session = clock->GetSession();
  226.     ODNameResolver*         resolver = session->GetNameResolver(ev);
  227.     DescType                propID;
  228.     ClockPropAccessor*        accessorObj;
  229.     ODError                    error = noErr;
  230.     AEDesc                    realData = NULL_DESCRIPTOR_DEFINITION;
  231.     AEDesc                    tokenDesc = NULL_DESCRIPTOR_DEFINITION;
  232.  
  233.     TRY
  234.  
  235.     if (form != formPropertyID)
  236.         THROW(errAEEventNotHandled);
  237.  
  238.     THROW_IF_ERROR( ODDescToAEDesc( selectionData, &realData ) );
  239.  
  240.     switch(propID = **(DescType**)(realData.dataHandle))
  241.     {
  242.         case pFont:
  243.         case pPointSize:
  244.         case pTextStyles:
  245.         case pDisplayMode:
  246.         case pASTime:
  247.             if ( !resolver->IsODToken( ev, value ) )
  248.                 THROW( errAEEventNotHandled /*errAENoSuchObject*/ );
  249.             tokenDesc.descriptorType = cProperty;
  250.             tokenDesc.dataHandle = NewHandle(sizeof(accessorObj));
  251.             THROW_IF_ERROR(MemError());
  252.  
  253.             accessorObj = new ClockPropAccessor(propID, clock);
  254.             SETFIRSTBYTESOFHANDLE(tokenDesc.dataHandle, ClockPropAccessor*,
  255.                     accessorObj);
  256.             
  257.             ODDesc* userODDesc = resolver->GetUserToken(ev, value);
  258.             THROW_IF_ERROR( AEDescToODDesc( &tokenDesc, userODDesc ) );
  259.             break;
  260.         default:
  261.             THROW(errAEEventNotHandled);
  262.             break;
  263.     }
  264.     
  265.     CATCH_ALL
  266.         error = ErrorCode();
  267.     ENDTRY
  268.     
  269.     AEDisposeDesc( &tokenDesc );
  270.     AEDisposeDesc( &realData );
  271.     return error;
  272. }    // GetPropertyFromNULL()
  273.  
  274.  
  275.  
  276. //------------------------------------------------------------------------------
  277. // HandleSetData
  278. //------------------------------------------------------------------------------
  279.  
  280. static pascal ODError HandleSetData(ODPart*            part,
  281.                                     ODAppleEvent*    message,
  282.                                     ODAppleEvent*    reply,
  283.                                     ODSLong            handlerRefcon)
  284. {
  285.     ODUnused(part);
  286.     ODUnused(reply);
  287.  
  288.     AEDesc theData = NULL_DESCRIPTOR_DEFINITION;
  289.     AEDesc evtDp = NULL_DESCRIPTOR_DEFINITION;
  290.     AEDesc theToken = NULL_DESCRIPTOR_DEFINITION;
  291.     AppleEvent realMessage = NULL_DESCRIPTOR_DEFINITION;
  292.     AppleEvent realReply = NULL_DESCRIPTOR_DEFINITION;
  293.     ODError    error = noErr;
  294.     
  295.     Environment*            ev = somGetGlobalEnvironment();
  296.     ClockPart*                clock = (ClockPart*)handlerRefcon;
  297.     ODSession*                session = clock->GetSession();
  298.     ODNameResolver*         resolver = session->GetNameResolver(ev);
  299.  
  300.     THROW_IF_ERROR( ODDescToAEDesc(message, &realMessage) );
  301.     THROW_IF_ERROR( ODDescToAEDesc(reply, &realReply ));
  302.  
  303.     ODOSLToken* tmpWrapper = kODNULL;
  304.     TRY
  305.     
  306.     THROW_IF_ERROR(AEGetKeyDesc(&realMessage, keyAEData,
  307.               typeWildCard, &theData));
  308.  
  309.     GetDirectParam( session, &realMessage, &evtDp);
  310.     tmpWrapper = new ODOSLToken();
  311.     THROW_IF_NULL(tmpWrapper);
  312.     tmpWrapper->InitODOSLToken(ev);
  313.     THROW_IF_ERROR( AEDescToODDesc(&evtDp, tmpWrapper ) );
  314.     if ( !resolver->IsODToken( ev, tmpWrapper ) )
  315.         THROW( errAENoSuchObject );
  316.     ODDesc* myTokenODDesc;
  317.     myTokenODDesc = resolver->GetUserToken(ev, tmpWrapper);
  318.     error = ODDescToAEDesc(myTokenODDesc, &theToken );
  319.  
  320.     if (error == noErr)
  321.     {
  322.         switch (theToken.descriptorType)
  323.         {
  324.             case typeProperty:
  325.                 ClockPropAccessor* embedPropAccessorObj =
  326.                         FIRSTBYTESFROMHANDLE(theToken.dataHandle, ClockPropAccessor*);
  327.                 embedPropAccessorObj->SetData(&theData);
  328.                 delete embedPropAccessorObj;
  329.                 break;
  330.             
  331.             default:
  332.                 ASSERTM(false, errAECantSupplyType, "You shouldn't be here");
  333.             
  334.         }
  335.     }
  336.     THROW_IF_ERROR(error);
  337.  
  338.     CATCH_ALL
  339.         error = ErrorCode();
  340.     ENDTRY
  341.     
  342.     AEDisposeDesc( &theToken );
  343.     AEDisposeDesc( &theData );
  344.     AEDisposeDesc( &evtDp );
  345.     AEDisposeDesc( &realMessage );
  346.     AEDisposeDesc( &realReply );
  347.     ODDeleteObject( tmpWrapper );
  348.     return error;
  349. }    // HandleSetData()
  350.  
  351.  
  352. //------------------------------------------------------------------------------
  353. // HandleGetData
  354. //------------------------------------------------------------------------------
  355.  
  356. static pascal ODError HandleGetData(ODPart*            part,
  357.                                     ODAppleEvent*    message,
  358.                                     ODAppleEvent*    reply,
  359.                                     ODSLong            handlerRefcon)
  360. {
  361. ODUnused(part);
  362. ODUnused(handlerRefcon);
  363.  
  364.     DescType                reqType;
  365.     Size                    theSize;
  366.     ODError                    error;
  367.     Environment*            ev = somGetGlobalEnvironment();
  368.     ClockPart*                clock = (ClockPart*)handlerRefcon;
  369.     ODSession*                session = clock->GetSession();
  370.     ODNameResolver*         resolver = session->GetNameResolver(ev);
  371.  
  372.     AEDesc                    objectData = NULL_DESCRIPTOR_DEFINITION;
  373.     AEDesc                    evtDp = NULL_DESCRIPTOR_DEFINITION;
  374.     AEDesc                    token = NULL_DESCRIPTOR_DEFINITION;
  375.     AppleEvent                realMessage = NULL_DESCRIPTOR_DEFINITION;
  376.     AppleEvent                realReply = NULL_DESCRIPTOR_DEFINITION;
  377.     THROW_IF_ERROR( ODDescToAEDesc(message, &realMessage));
  378.     THROW_IF_ERROR( ODDescToAEDesc(reply, &realReply));
  379.  
  380.     ODOSLToken* tmpWrapper = kODNULL;
  381.     TRY
  382.  
  383.     GetDirectParam( session, &realMessage, &evtDp);
  384.     tmpWrapper = new ODOSLToken();
  385.     THROW_IF_NULL(tmpWrapper);
  386.     tmpWrapper->InitODOSLToken(ev);
  387.     THROW_IF_ERROR( AEDescToODDesc(&evtDp, tmpWrapper ) );
  388.     if ( !resolver->IsODToken( ev, tmpWrapper ) )
  389.         THROW( errAENoSuchObject );
  390.     ODDesc* myTokenODDesc;
  391.     myTokenODDesc = resolver->GetUserToken(ev, tmpWrapper);
  392.     THROW_IF_ERROR( ODDescToAEDesc(myTokenODDesc, &token ) );
  393.  
  394.             //    Next, get the requested return type, if it exists.
  395.     error = AEGetParamPtr(&realMessage, keyAERequestedType, typeType,
  396.                     &reqType, (Ptr)&reqType, sizeof(reqType), &theSize);
  397.     if (error == errAEDescNotFound)        // Not an error if return type is not found.
  398.     {
  399.         error = noErr;
  400.         reqType = typeWildCard;
  401.     }
  402.     THROW_IF_ERROR(error);
  403.  
  404.     switch (token.descriptorType)
  405.     {
  406.         case typeProperty:
  407.             ClockPropAccessor* propAccessorObj = FIRSTBYTESFROMHANDLE(
  408.                     token.dataHandle, ClockPropAccessor*);
  409.             propAccessorObj->GetData(&objectData);
  410.             delete propAccessorObj;
  411.             break;
  412.         default:
  413.             ASSERTM(false, errAECantSupplyType, "You shouldn't be here");
  414.         
  415.     }
  416.     
  417.     error = AEPutParamDesc( &realReply, keyAEResult, &objectData );
  418.     THROW_IF_ERROR(error);
  419.     THROW_IF_ERROR( AEDescToODDesc( &realReply, reply ) );
  420.  
  421.     CATCH_ALL
  422.         error = ErrorCode();
  423.     ENDTRY
  424.     
  425.     AEDisposeDesc( &token );
  426.     AEDisposeDesc( &evtDp );
  427.     AEDisposeDesc( &objectData );
  428.     AEDisposeDesc( &realMessage );
  429.     AEDisposeDesc( &realReply );
  430.     ODDeleteObject( tmpWrapper );
  431.     return error;
  432. }    // HandleGetData()
  433.  
  434.  
  435. //------------------------------------------------------------------------------
  436. // ClockCompare
  437. // For now, just handle what we need to handle
  438. //------------------------------------------------------------------------------
  439. static pascal ODError ClockCompare(    ODPart*            thePart,
  440.                                     DescType        oper,
  441.                                     ODOSLToken*        obj1,
  442.                                     ODOSLToken*        obj2,
  443.                                     ODBoolean*        result,
  444.                                     ODSLong            refCon)
  445. {
  446.     ODUnused(thePart);
  447.     ODUnused(obj1);
  448.     ODUnused(result);
  449.     ODUnused(refCon);
  450.  
  451.     ODError    error = noErr;
  452.     Environment*    ev = somGetGlobalEnvironment();
  453.     ClockPart*        clock = (ClockPart*)refCon;
  454.     ODNameResolver* resolver = clock->GetSession()->GetNameResolver(ev);
  455.     ODBoolean isChar = kODFalse;
  456.     ODDesc* tokenWrapper = kODNULL;    
  457.     AEDesc realToken;
  458.     
  459.     switch(oper)
  460.     {
  461.         case kAEEquals:
  462.             tokenWrapper = resolver->GetUserToken(ev, obj2);
  463.             error = ODDescToAEDesc( tokenWrapper, &realToken );
  464.             isChar = realToken.descriptorType == typeChar;
  465.             AEDisposeDesc( &realToken );
  466.             if (isChar)
  467.                 break;
  468.         default:
  469.             error = errAEEventNotHandled;
  470.     }
  471.     
  472.     return error;
  473. }
  474.  
  475. //------------------------------------------------------------------------------
  476. // ClockCount
  477. //------------------------------------------------------------------------------
  478. static pascal ODError ClockCount(    ODPart*            thePart,
  479.                                     DescType        desiredType,
  480.                                     DescType        containerClass,
  481.                                     ODOSLToken*        container,
  482.                                     ODSLong*        result,
  483.                                     ODSLong            refCon)
  484. {
  485.     ODUnused(thePart);
  486.     ODUnused(desiredType);
  487.     ODUnused(containerClass);
  488.     ODUnused(container);
  489.     ODUnused(result);
  490.     ODUnused(refCon);
  491.     return errAEEventNotHandled;
  492. }
  493.  
  494. //------------------------------------------------------------------------------
  495. // ClockDisposeToken
  496. //------------------------------------------------------------------------------
  497. static pascal ODError ClockDisposeToken(ODPart*        thePart,
  498.                                         ODOSLToken*    unneededToken,
  499.                                         ODSLong        refCon)
  500. {
  501.     ODUnused(thePart);
  502.     ODUnused(unneededToken);
  503.     ODUnused(refCon);
  504.     return errAEEventNotHandled;
  505. }
  506.  
  507. //------------------------------------------------------------------------------
  508. // Utility functions
  509. //------------------------------------------------------------------------------
  510.  
  511. void GetDirectParam(ODSession* session, AppleEvent* message,
  512.         AEDesc* evtDp)
  513. {
  514.     Environment* ev = somGetGlobalEnvironment();
  515.     AEDesc localDP;
  516.     THROW_IF_ERROR(AEGetParamDesc(message, keyDirectObject, typeWildCard,
  517.             &localDP));
  518.  
  519.     ODNameResolver* resolver = session->GetNameResolver(ev);
  520.  
  521.     ODOSLToken* tmpWrapper = new ODOSLToken();
  522.     THROW_IF_NULL(tmpWrapper);
  523.     tmpWrapper->InitODOSLToken(ev);
  524.     THROW_IF_ERROR( AEDescToODDesc(&localDP, tmpWrapper ) );
  525.  
  526.     ODBoolean isToken = resolver->IsODToken(ev, tmpWrapper);
  527.     ODDeleteObject( tmpWrapper );
  528.     if ( isToken )
  529.     {
  530.         *evtDp = localDP;
  531.     }
  532.     else
  533.     {
  534.         AEDisposeDesc(&localDP);
  535.         WARN("RlShlEv.cpp: GetDirectParam. Found a non-token.  About to throw.");
  536.         THROW( errAEEventNotHandled );
  537.     }
  538. }    // GetDirectParam()
  539.  
  540.  
  541. //------------------------------------------------------------------------------
  542. // CreatePartObjectSpec
  543. //------------------------------------------------------------------------------
  544.  
  545. static void CreatePartObjectSpec(DescType keyForm, AEDesc* containerObjSpec,
  546.         AEDesc *objectSpec)
  547. {
  548.     // We'll really return a spec to the FRAME containing the part.
  549.     // This cause you can get to the part from the frame, but not the other
  550.     // way 'round.
  551.     
  552.     AEDesc keyData;
  553.     
  554.     switch(keyForm)
  555.     {
  556.         case formAbsolutePosition :
  557.             long index = 1;            // <eeh> we should figure out where
  558.             THROW_IF_ERROR(AECreateDesc(typeLongInteger, &index, sizeof(index),
  559.                     &keyData));
  560.             break;
  561.         default :
  562.             ASSERTM(false, -1, "Can't handle that form!!! (yet)");
  563.     }
  564.  
  565.     CreateObjSpecifier(cPart, containerObjSpec, keyForm, &keyData, true,
  566.             objectSpec);
  567. }    // CreatePartObjectSpec()
  568.  
  569.  
  570.